package cn.com.hd.mall.web.webservices.entity.response.shoppingcart;

import java.util.ArrayList;
import java.util.List;

/**
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-03-20 11:12
 *
 * <p>
 * 促销区信息的 wrap
 * </p>
 */
public class WrapPromotionBean {
    // 增补字段
    private String msgGift;
    private String msgReduce;
    // 商家和促销对象的促销对象集合
    private List<MerchWraper> merchWraperList;

    public boolean isValid() {
        if (null == merchWraperList) {
            return false;
        } else {
            boolean isValid = false;
            for (MerchWraper merchWraper : merchWraperList) {
                isValid = merchWraper.isValid();

                if (isValid) {
                    break;
                }
            }
            return isValid;
        }
    }

    public String getMsgGift() {
        return countAllGift();
    }

    public void setMsgGift(String msgGift) {
        this.msgGift = msgGift;
    }


    private String countAllGift() {
        if (null == merchWraperList || merchWraperList.isEmpty()) {
            return null;
        }

        List<GiftListBean> giftList = new ArrayList<>();
        for (MerchWraper merchWraper : merchWraperList) {
            if (merchWraper.isSelected() && "0".equals(merchWraper.getPromotionType()) && null != merchWraper.getGiftList()) {
                for (GiftListBean gift : merchWraper.getGiftList()) {
                    if (giftList.contains(gift)) {
                        int oldVal = giftList.get(giftList.indexOf(gift)).getGiftNum();
                        giftList.get(giftList.indexOf(gift)).setGiftNum(oldVal + gift.getGiftNum());
                    } else {
                        giftList.add(gift);
                    }
                }
            }
        }

        if (giftList.isEmpty()) {
            return null;
        } else {
            StringBuilder giftStr = new StringBuilder();

            for (GiftListBean gift : giftList) {
                giftStr.append("送").append(gift.getGiftName()).append(gift.getGiftNum()).append(gift.getSpecName()).append(";");
            }
            return giftStr.toString();
        }
    }

    public String getMsgReduce() {
        return msgReduce;
    }

    public void setMsgReduce(String msgReduce) {
        this.msgReduce = msgReduce;
    }

    public List<MerchWraper> getMerchWraperList() {
        if (null == merchWraperList) {
            merchWraperList = new ArrayList<>();
        }
        return merchWraperList;
    }

    public void setMerchWraperList(List<MerchWraper> merchWraperList) {
        this.merchWraperList = merchWraperList;
    }
}
